1 module hip.windowing.window;
2 
3 version(Android){}
4 else version(linux) version = X11;
5 
6 version(UWP){}
7 else version(Windows) version = WindowsNative;
8 
9 
10 enum HipWindowFlags
11 {
12     RESIZABLE   = 1,
13     FULLSCREEN  = 1<<1,
14     MAXIMIZABLE = 1<<2,
15     MINIMIZABLE = 1<<4,
16 
17     DEFAULT = RESIZABLE | MAXIMIZABLE | MINIMIZABLE
18 }
19 
20 /**
21 *   This is not a feature complete windowing abstraction. It only has the necessary resources
22 *   for making the engine work. Its feature is not to be an implementation as complete as SDL.
23 *   thus, reducing the external dependencies, binary size and compilation steps.
24 */
25 class HipWindow
26 {
27     int width, height;
28     HipWindowFlags flags;
29 
30     string[] errors;
31 
32     version(WindowsNative)
33     {
34         import hip.windowing.platforms.windows;
35         HWND hwnd;
36     }
37     else version(X11)
38     {
39         import hip.windowing.platforms.x11;
40     }
41     else version(WebAssembly)
42     {
43         import hip.windowing.platforms.browser;
44     }
45     else version(AppleOS)
46     {
47         import hip.windowing.platforms.appleos;
48         void* MTKView;
49     }
50     else
51     {
52         import hip.windowing.platforms.null_;
53     }
54 
55     this(int width, int height, HipWindowFlags flags)
56     {
57         this.width = width;
58         this.height = height;
59         this.flags = flags;
60     }
61     void start()
62     {
63         version(WindowsNative)
64             openWindow(hwnd, width, height);
65         else version(WebAssembly)
66         {
67             openWindow(width, height);
68         }
69         else version(AppleOS)
70         {
71             openWindow(&MTKView, width, height);
72         }
73         else version(X11)
74         {
75             version(SharedX11)
76                 loadX11();
77             openWindow(width, height);
78         }
79     }
80     bool startOpenGLContext(int majorVersion = 3, int minorVersion = 3)
81     {
82         //Windows must reinitialize the window if it uses modern gl, so, it must update the window here
83         version(WindowsNative)
84             return hip.windowing.platforms.windows.initializeOpenGL(hwnd, majorVersion, minorVersion);
85         else version(X11)
86             return hip.windowing.platforms.x11.initializeOpenGL(majorVersion, minorVersion);
87         else
88             return true; //Assume that OpenGL is started
89     }
90     bool destroyOpenGLContext()
91     {
92         return destroy_GL_Context();
93     }
94     void pollWindowEvents(){poll();}
95     void rendererPresent()
96     {
97         swapBuffer();
98     }
99     void setName(string name)
100     {
101         version(WindowsNative)
102             hip.windowing.platforms.windows.setWindowName(hwnd, name);
103         else version(X11)
104             hip.windowing.platforms.x11.setWindowName(name);
105         else version(AppleOS)
106             hip.windowing.platforms.appleos.setWindowName(name);
107         else
108             errors~= "setName is not implemented for this platform";
109     }
110     void setSize(uint width, uint height)
111     {
112         version(WindowsNative)
113             hip.windowing.platforms.windows.setWindowSize(hwnd, width, height);
114         else version(X11)
115             return hip.windowing.platforms.x11.setWindowSize(width, height);
116         else version(AppleOS)
117             return hip.windowing.platforms.appleos.setWindowSize(width, height);
118         else version(WebASsembly)
119             return hip.windowing.platforms.browser.setWindowSize(width, height);
120         else
121             errors~= "setSize is not implemented for this platform";
122     }
123     int[2] getSize()
124     {
125         version(WindowsNative)
126             return hip.windowing.platforms.windows.getWindowSize(hwnd);
127         else version(X11)
128             return hip.windowing.platforms.x11.getWindowSize();
129         else version(WebAssembly)
130             return hip.windowing.platforms.browser.getWindowSize();
131         else version(AppleOS)
132             return hip.windowing.platforms.appleos.getWindowSize();
133         else
134         {
135             errors~= "getSize is not implemented for this platform";
136             return [0,0];
137         }
138     }
139     void setVSyncActive(bool active)
140     {
141          //Windows must reinitialize the window if it uses modern gl, so, it must update the window here
142         version(WindowsNative)
143             hip.windowing.platforms.windows.setVsyncActive(active);
144         else version(X11)
145             hip.windowing.platforms.x11.setVsyncActive(active);
146         else
147             errors~= "VSync is not implemented for this platform";
148 
149     }
150     void setFullscreen(bool fullscreen)
151     {
152         version(AppleOS)
153             hip.windowing.platforms.appleos.setFullscreen(fullscreen);
154         else
155             errors~= "Fullscreen is not implemented for this platform";
156     }
157     
158     void show()
159     {
160         version(WindowsNative)
161             return hip.windowing.platforms.windows.show(hwnd);
162         else version(X11)
163             return hip.windowing.platforms.x11.show();
164         else version(WebAssembly){} //Has no show
165         else version(AppleOS){} //Has no show
166         else
167             errors~= "Show is not implemented for this platform";
168     }
169     void hide(){}
170     void exit()
171     {
172         version(SharedX11)
173             unloadX11();
174     } 
175 
176 
177 }